﻿// async_client.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "async_tcp_client.h"
#include <iostream>
#include <thread>
#include "ConfigMgr.h"
int main(int argc,char* argv[]) {
    try {
        //std::string host = ConfigMgr::Inst().GetValue("Server","Host");
		//std::string port = ConfigMgr::Inst().GetValue("Server", "Port");
		std::string client_id = (argc > 1) ? argv[1] : "Client001";
        std::string config_path = (argc > 2) ? argv[2] : "config.ini";
        ConfigMgr::Inst().LoadFromFile(config_path);
        std::cout << "实际读取的配置文件路径：" << boost::filesystem::absolute(config_path) << std::endl;
        std::string host = ConfigMgr::Inst().GetValue("Server", "Host");
        std::string port = ConfigMgr::Inst().GetValue("Server", "Port");
        std::cout << "=== 客户端 [" << client_id << "] 启动 ===" << std::endl;
        std::cout << "配置文件：" << config_path << std::endl;
        std::cout << "连接目标：" << host << ":" << port << std::endl;

        boost::asio::io_context io_context;

        AsyncTcpClient client(io_context, host, port,client_id);
        client.Start();

        std::thread io_thread([&io_context]() {
            io_context.run();
            });

        io_thread.join(); 
    }
    catch (std::exception& e) {
        std::cerr << "发生错误：" << e.what() << std::endl;
        return 1;
    }

    return 0;
}
